package com.king.headline.net;

import android.content.Context;
import android.text.TextUtils;


import com.king.headline.util.Configuration;
import com.king.headline.util.UIUtils;
import com.king.headline.util.Utils;
import com.king.headline.util.encry.CryptoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class NetWork {
    String API = Configuration.API_HOST;
    public final String LOGIN = API + "app/tt/anno/login";//登录
    public final String PHONELOGIN = API + "app/tt/anno/phoneLogin";//快捷登录
    public final String RESETPASSWORD = API + "app/tt/anno/resetPassword";//重置密码
    public final String REGIST = API + "app/tt/anno/regist";//注册
    public final String CHANGEPASSWORD = API + "app/tt/changePassword";//修改密码
    public final String CHANGENICKNAME = API + "app/tt/setNickname";//设置昵称
    public final String GETPHONENUMBER = API + "vc/";//获取验证码
    public final String SETNICKPIC = API + "app/tt/setHeadPic";//设置头像
    public final String QUERYBANNERS = API + "app/tt/anno/carousels";//首页banner
    public final String GETAPPMAIN = API + "app/tt/anno/videos";//首页列表/详情页
    public final String VIDEOSCOMMENTS = API + "app/tt/videosComments";//评论
    public final String GETCOLLECTION = API + "app/tt/videos/collections";//收藏列表
    public final String SETCOLLECTION = API + "app/tt/videosCollections";//收藏
    public final String LOGIN3RD = API + "app/tt/anno/3rdLogin";//三方登录
    public final String UNBIND3RDLOGIN = API + "app/tt/unBind3rdLogin";//解绑三方登陆
    public final String VIDEOSZANS = API + "app/tt/videosZans";//点赞
    public final String BINDTELEPHONE = API + "app/tt/bindTelephone";//修改手机号
    public final String BINDTELEPHONEVALID = API + "vc/valid";//换绑手机号
    private static NetWork instance = null;
    private BaseApi mApiClient = null;

    private NetWork(Context context) {
        mApiClient = BaseApi.getInstance(context);
    }

    public static NetWork getInstance() {
        if (instance == null) {
            synchronized (NetWork.class) {
                if (instance == null) {
                    instance = new NetWork(UIUtils.getContext());
                }
            }
        }
        return instance;
    }


    /**
     * 登录接口实现
     *
     * @param password
     * @param callback
     */
    public void login(String userName, String password, com.king.headline.net.Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userName", userName);
            jsonObject.put("passWord", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(LOGIN, params, callback);
    }

    public void login(String apikey, com.king.headline.net.Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("apikey", apikey);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(LOGIN, params, callback);
    }

    public void modifyPsw(String encyptdId ,String apikey,String oldPassWord,String passWord, com.king.headline.net.Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("encyptdId", encyptdId);
            jsonObject.put("apikey", apikey);
            jsonObject.put("oldPassWord", oldPassWord);
            jsonObject.put("passWord", passWord);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(CHANGEPASSWORD, params, callback);
    }

    /**
     * 重置密码
     *
     * @param callback
     */
    public void resetPassword(String phone, String code,String passWord, Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phoneNumber", phone);
            jsonObject.put("codeNum", code);
            jsonObject.put("passWord", passWord);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(RESETPASSWORD, params, callback);
    }
    /**
     * 注册
     *
     * @param callback
     */
    public void regist(String phone, String code,String passWord, Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phoneNumber", phone);
            jsonObject.put("codeNum", code);
            jsonObject.put("passWord", passWord);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(REGIST, params, callback);
    }

    /**
     * 修改昵称
     * @param encyptdId
     * @param apikey
     * @param nickName
     * @param callback
     */
    public void modifyNickName(String encyptdId, String apikey, String nickName,Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("encyptdId", encyptdId);
            jsonObject.put("apikey", apikey);
            jsonObject.put("nickName", nickName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(CHANGENICKNAME, params, callback);

    }
    /**
     * 验证码登录接口实现
     *
     * @param callback
     */
    public void loginByPromote(String phone, String code, Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phoneNumber", phone);
            jsonObject.put("codeNum", code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(PHONELOGIN, params, callback);
    }

    /**
     * 获取验证码
     *
     * @param callback
     */
    public void getCode(String phone, Callback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phoneNumber", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
//        HashMap<String, String> params = new HashMap<String, String>();
//        params.put("result", str);
        mApiClient.get(GETPHONENUMBER + str, callback);
    }

    /**
     * 上传头像
     *
     */
    public void setNickPic(final String encyptdId, final String apikey, final String picPath, final Callback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> param = new HashMap<String, String>();
                param.put("encyptdId", encyptdId);
                param.put("apikey", apikey);
                File file = new File(picPath);
                param.put("chatHead", file.toString());
                try {
                    String result = Utils.doPostPic(SETNICKPIC+"?encyptdId="+encyptdId+"&apikey=" +apikey, file);
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject jsonObject1 = jsonObject.optJSONObject("msg");
                    callback.success(result);
                } catch (Exception e) {
                    callback.failed(e.toString());
                    e.printStackTrace();
                }

            }
        }).start();

    }


    /**
     * 查询会员列表
     *
     * @param callback
     */
    public void queryClubMemberList(String text, String type, com.king.headline.net.Callback callback) {
    }


    public void getBanner(String encyptdId,String apikey, Callback callback) {
        mApiClient.get(QUERYBANNERS+"?encyptdId="+encyptdId+"&apikey="+apikey,callback);
    }

    public void getMain(String encyptdId, String apikey, int index, Callback callback) {
        HashMap<String, String> params = new HashMap<String,String>();
        if (!TextUtils.isEmpty(apikey)) {
            params.put("encyptdId", encyptdId);
            params.put("apikey", apikey);
        }
        if (index!=0){
            params.put("index", index+"");
        }
        mApiClient.get(GETAPPMAIN, params, callback);
    }
    public void getUserPage(String sourceId, int index, Callback callback) {
        HashMap<String, String> params = new HashMap<String,String>();
        params.put("sourceId", sourceId);
        if (index!=0){
            params.put("index", index+"");
        }
        mApiClient.get(GETAPPMAIN, params, callback);
    }
    public void getCollection(String encyptdId, String apikey, int index, Callback callback) {
        HashMap<String, String> params = new HashMap<String,String>();
        params.put("encyptdId", encyptdId);
        params.put("apikey", apikey);
        if (index!=0){
            params.put("index", index+"");
        }
        mApiClient.get(GETCOLLECTION, params, callback);
    }

    public void videosCollections(String encyptdId, String apikey, String sourceId, int type , Callback callback) {
        HashMap<String, String> params = new HashMap<String,String>();
        params.put("encyptdId", encyptdId);
        params.put("apikey", apikey);
        params.put("sourceId", sourceId);
        params.put("type", type+"");
        mApiClient.get(SETCOLLECTION, params, callback);
    }
    public void getDetail(String encyptdId, String apikey, String sourceId , Callback callback) {
        HashMap<String, String> params = new HashMap<String,String>();
        params.put("encyptdId", encyptdId);
        params.put("apikey", apikey);
        mApiClient.get(GETAPPMAIN + "/" + sourceId, params, callback);
    }


    public void comment(String encyptdId, String apikey, String sourceId, String replyID, String content, Callback callback) {
        HashMap<String, String> params = new HashMap<String,String>();
        params.put("encyptdId", encyptdId);
        params.put("apikey", apikey);
        params.put("sourceId", sourceId);
        params.put("content", content);
        if (!TextUtils.isEmpty(replyID)){
            params.put("replyTo", replyID);
        }
        mApiClient.post(VIDEOSCOMMENTS, params, callback);
    }
    public void VidoLike(String  sourceId, String encyptdId,String apikey,Callback callback) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("encyptdId", encyptdId);
        params.put("apikey", apikey);
        params.put("sourceId", sourceId);
        params.put("type", 1+"");//1-->点赞,2-->取消赞
        mApiClient.get(VIDEOSZANS, params, callback);
    }


    public void bindTelephone(String encyptdId,String apikey, String telephone, String codeNum, Callback callback) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject();
            jsonObject.put("encyptdId", encyptdId);
            jsonObject.put("telephone", telephone);
            jsonObject.put("type", 1);
            jsonObject.put("codeNum", codeNum);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        params.put("apikey", apikey);
        mApiClient.get(BINDTELEPHONE, params, callback);
    }

    public void valid(String telephone, String codeNum, Callback callback) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject();
            jsonObject.put("phoneNumber", telephone);
            jsonObject.put("codeNum", codeNum);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String str = CryptoUtil.encrypt(jsonObject.toString());
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("result", str);
        mApiClient.get(BINDTELEPHONEVALID, params, callback);
    }
}